package com.artron.mediaartron.ui.fragment.center;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.artron.baselib.base.BaseStaticFragment;
import com.artron.baselib.entity.Response;
import com.artron.baselib.utils.ToastUtil;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.config.AppProfile;
import com.artron.mediaartron.data.entity.User;
import com.artron.mediaartron.data.net.RetrofitHelper;
import com.artron.mediaartron.data.net.api.LoginApi;
import com.artron.mediaartron.ui.activity.LoginActivity;
import com.artron.mediaartron.ui.helper.ThrottleFirstClickHelper;
import com.artron.mediaartron.ui.activity.GeneralActivity;

import butterknife.BindView;
import retrofit2.http.HEAD;
import rx.functions.Action1;

/**
 * Class description here
 *
 * @author doing
 * @version 1.0.0
 * @since 2017-11-11.
 */

public class ChangePasswordFragment extends BaseStaticFragment implements View.OnClickListener {

    @BindView(R.id.ChangePasswordFragment_et_old_pass)
    protected EditText mEtOldPass;
    @BindView(R.id.ChangePasswordFragment_et_new_pass)
    protected EditText mEtNewPass;
    @BindView(R.id.ChangePasswordFragment_tv_forget)
    protected TextView mTvForget;
    @BindView(R.id.ChangePasswordFragment_btn_ensure)
    protected Button mBtnEnsure;
    @BindView(R.id.ChangePasswordFragment_iv_passIsVisible)
    protected ImageView mPassIsVisible;
    private boolean isShowPassword = true;

    public static ChangePasswordFragment newInstance() {
        Bundle args = new Bundle();
        ChangePasswordFragment fragment = new ChangePasswordFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_change_password;
    }

    @Override
    protected void initView() {
        ThrottleFirstClickHelper.throttleFirst(mTvForget, this);
        ThrottleFirstClickHelper.throttleFirst(mBtnEnsure, this);
        mPassIsVisible.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ChangePasswordFragment_iv_passIsVisible:
                if (isShowPassword) {
                    mEtNewPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    mPassIsVisible.setImageResource(R.drawable.ic_password_visible);
                } else {
                    mEtNewPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    mPassIsVisible.setImageResource(R.drawable.ic_password_not_visible);
                }
                isShowPassword = !isShowPassword;
                break;
            case R.id.ChangePasswordFragment_tv_forget://忘记旧密码

                GeneralActivity.start(mContext, GeneralActivity.FORGOT_PASSWORD, "重置密码");
//                LoginActivity.start(mContext, LoginActivity.TYPE_RESET);
                break;
            case R.id.ChangePasswordFragment_btn_ensure:
                changePassword();
                break;
        }
    }

    private void changePassword() {
        boolean login = AppProfile.getUserInfo().isLogin();
        if (login) {
            String oldPassword = mEtOldPass.getText().toString();
            String newPassword = mEtNewPass.getText().toString();
            if (isNull(oldPassword, "请输入旧密码") && isNull(newPassword, "请输入新密码")) {

                User user = AppProfile.getUserInfo().getUser();
                LoginApi api = RetrofitHelper.getLoginApi();
                RetrofitHelper.subscript(api.modifyPwd(user.getUtoken(), oldPassword, newPassword),
                        new Action1<Response>() {
                            @Override
                            public void call(Response response) {
                                ToastUtil.show(response.getMessage());
                            }
                        });
            }
        }
    }

    private boolean isNull(String pwd, String msg) {
        if (TextUtils.isEmpty(pwd)) {
            ToastUtil.show(msg);
            return false;
        } else {
            return true;
        }
    }
}
